package edu.gdkm.config;

import edu.gdkm.config.SpringConfig;
import edu.gdkm.config.SpringMvcConfig;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;


/** * 代替web.xml在tomcat服务器启动的时候加载spring及springmvc的容器 */
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**     * 指定spring的配置类
     * @return
     */
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    /**     * 指定SpringMVC的配置类
     * @return
     */
    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    /**     * 指定DispatcherServlet的映射规则，即url-pattern
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceRequestEncoding(true);
        return new Filter[]{encodingFilter};
    }
}